package com.com.knowledgepaymentmanagementsystem.controller.course;
import com.com.knowledgepaymentmanagementsystem.pojo.course.Video;
import com.com.knowledgepaymentmanagementsystem.service.course.VideoService;
import com.com.knowledgepaymentmanagementsystem.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// 允许跨域调度
@CrossOrigin

@Api(tags = "视频管理")
// 声明控制层
@RestController
// 类映射
@RequestMapping("video")
public class VideoController {
    // 注入 VideoService 接口
    @Autowired
    private VideoService videoService;

    // 查询所有视频数据
    @ApiOperation(value = "查询视频", httpMethod = "POST", notes = "查询所有视频条目")
    @PostMapping("findVideoList")
    public ResultUtil<List<Video>> findVideoList(@RequestBody Video video) {

        try {
            return new ResultUtil<>(200, "查询视频数据成功", videoService.findVideoList(video));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtil<>(500, "查询视频数据失败");
        }
    }

    // 删除用户表中数据（根据 ID 删除）
    @ApiOperation(value = "删除视频", httpMethod = "DELETE", notes = "根据ID删除视频")
    @DeleteMapping("delVideoById")
    public ResultUtil delVideoById(@RequestParam Integer id) {
        try {

            // 处理请求
            videoService.delVideoById(id);
            return new ResultUtil<>(200, "删除视频成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtil<>(500, "删除视频失败");
        }

    }

    // 添加用户
    @ApiOperation(value = "添加视频", httpMethod = "POST", notes = "添加视频")
    @PostMapping("addVideo")
    public ResultUtil addVideo(@RequestBody Video video) {

        try {
            // 处理添加请求
            videoService.addVideo(video);
            return new ResultUtil<>(200, "添加视频成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtil<>(500, "添加视频失败");
        }
    }

    // 修改音频数据
    @Transactional
    @ApiOperation(value = "修改视频", httpMethod = "PUT", notes = "修改视频")
    @PutMapping("updataVideo")
    public ResultUtil updataVideo(@RequestBody Video video) {

        try {
            // 处理修改请求
            videoService.updateVideo(video);
            return new ResultUtil<>(200, "修改视频成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtil<>(500, "修改视频失败");
        }
    }
}
